name: Go Related Checks

# Any change in triggers needs to be reflected in the concurrency group.
on:
  pull_request: {}
  push:
    branches:
      - main
      - ft/main/**
  # Add this workflow to be triggered by merge queue events
  merge_group:

permissions: read-all

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.event.after || github.event.merge_group && github.run_id }}
  cancel-in-progress: ${{ !github.event.merge_group }}

jobs:
  go-mod:
    name: Check Go Modules
    runs-on: ubuntu-latest
    steps:
      - name: Install Go
        uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
        with:
          # renovate: datasource=golang-version depName=go
          go-version: 1.22.0
      - name: Checkout code
        uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
        with:
          persist-credentials: false
      - name: Check module vendoring
        run: |
          go mod tidy
          go mod vendor
          test -z "$(git status --porcelain)" || (echo "please run 'go mod tidy && go mod vendor', and submit your changes"; exit 1)

  golangci:
    name: Lint Source Code
    runs-on: ubuntu-latest
    steps:
      - name: Install Go
        uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
        with:
          # renovate: datasource=golang-version depName=go
          go-version: 1.22.0
      - name: Checkout code
        uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
        with:
          persist-credentials: false
      - name: Run golangci-lint
        uses: golangci/golangci-lint-action@3cfe3a4abbb849e10058ce4af15d205b6da42804 # v4.0.0
        with:
          # renovate: datasource=docker depName=golangci/golangci-lint
          version: v1.56.2
          skip-cache: true
          args: "--out-${NO_FUTURE}format colored-line-number --verbose --modules-download-mode=vendor"

  precheck:
    runs-on: ubuntu-latest
    name: Precheck
    steps:
      - name: Install Go
        uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
        with:
          # renovate: datasource=golang-version depName=go
          go-version: 1.22.0
      - name: Checkout code
        uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
        with:
          persist-credentials: false
          # hard-code the path instead of using ${{ github.repository }} to make sure it works for forked repo as well
          path: src/github.com/cilium/cilium
      - name: Go code prechecks
        run: |
          cd src/github.com/cilium/cilium
          make precheck

  generate-api:
    runs-on: ubuntu-latest
    name: Generate API
    steps:
      - name: Install Go
        uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
        with:
          # renovate: datasource=golang-version depName=go
          go-version: 1.22.0
      - name: Checkout code
        uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
        with:
          persist-credentials: false
          # hard-code the path instead of using ${{ github.repository }} to make sure it works for forked repo as well
          path: src/github.com/cilium/cilium
      - name: Check api generated files
        run: |
          cd src/github.com/cilium/cilium
          contrib/scripts/check-api-code-gen.sh

  generate-k8s-api:
    runs-on: ubuntu-latest
    name: Generate k8s API
    steps:
      - name: Install Go
        uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
        with:
          # renovate: datasource=golang-version depName=go
          go-version: 1.22.0
      - name: Checkout code
        uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
        with:
          persist-credentials: false
          # hard-code the path instead of using ${{ github.repository }} to make sure it works for forked repo as well
          path: src/github.com/cilium/cilium
      - name: Install protobuf dependencies
        env:
          PROTOBUF_VERSION: 3.12.4
        run: |
          curl -Lo protoc-$PROTOBUF_VERSION-linux-x86_64.zip https://github.com/protocolbuffers/protobuf/releases/download/v$PROTOBUF_VERSION/protoc-$PROTOBUF_VERSION-linux-x86_64.zip
          unzip protoc-$PROTOBUF_VERSION-linux-x86_64.zip
          sudo chmod +x bin/protoc && sudo cp bin/protoc /usr/local/bin
      - name: Check k8s generated files
        run: |
          # Set GOBIN to ensure 'go install' binaries end up in the same directory
          # as the one actions/setup-go adds to PATH, regardless of GOPATH.
          export GOBIN="$HOME/go/bin"

          cd src/github.com/cilium/cilium
          contrib/scripts/check-k8s-code-gen.sh
